package org.shreker.shan.mgrsite.buiness.listener;

import org.shreker.shan.core.domain.Login;
import org.shreker.shan.core.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

@Component
public class MgrContextListener implements ApplicationListener<ContextRefreshedEvent> {
	
	@Autowired
	private ILoginService loginService;
	
	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		Long count = loginService.selectCountByIdentity(Login.IDENTITY_MGR);
		if (count == 0) { // 创建管理员
			try {
				Login login = new Login();
				login.setIdentity(Login.IDENTITY_MGR);
				login.setUsername("admin");
				login.setPassword("admin");
				loginService.save(login);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}
